package admin;

import java.io.Serializable;
import java.util.List;
import java.util.ResourceBundle;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;







import dtos.MeasureunitDTO;
import dtos.ResticketDTO;
import utils.MeasureunitDAORemote;
import utils.ResticketDAORemote;

/*
 * author: sotocs
 */
@ManagedBean
@SessionScoped
public class EditMeasureunitsBean implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	ResourceBundle bundle = ResourceBundle.getBundle(
			"languageSettings.language", FacesContext.getCurrentInstance()
					.getViewRoot().getLocale());
	
	List<MeasureunitDTO> listaMU;
	@EJB
	MeasureunitDAORemote muDAO;
	@EJB
	ResticketDAORemote resDAO;
	
	String nameMU;
	String message = "";
	private String msgSuccessDel = bundle.getString("Beans_admin_editMeasureunitsBean_msgSuccessDel");
	private String msgSuccessAdd = bundle.getString("Beans_admin_editMeasureunitsBean_msgSuccessAdd");
	MeasureunitDTO measureunitDTO;

	
	public String getMsgSuccessDel() {
		return msgSuccessDel;
	}


	public void setMsgSuccessDel(String msgSuccessDel) {
		this.msgSuccessDel = msgSuccessDel;
	}


	public String getMsgSuccessAdd() {
		return msgSuccessAdd;
	}


	public void setMsgSuccessAdd(String msgSuccessAdd) {
		this.msgSuccessAdd = msgSuccessAdd;
	}


	public String getMessage() {
		return message;
	}


	public void setMessage(String message) {
		this.message = message;
	}


	public MeasureunitDTO getMeasureunitDTO() {
		return measureunitDTO;
	}


	public void setMeasureunitDTO(MeasureunitDTO measureunitDTO) {
		this.measureunitDTO = measureunitDTO;
	}


	public String getNameMU() {
		return nameMU;
	}


	public void setNameMU(String nameMU) {
		this.nameMU = nameMU;
	}


	public List<MeasureunitDTO> getListaMU() {
		listaMU=getAllMU();
		return listaMU;
	}


	public void setListaMU(List<MeasureunitDTO> listaMU) {
		this.listaMU = listaMU;
	}

	/*
	 * getAll
	 */
	public List<MeasureunitDTO> getAllMU(){
		return muDAO.getAllMeasureunits();
	}
	/*
	 * insert 
	 */
	public void insertMeasureunitDTO(){
		measureunitDTO = new MeasureunitDTO();
		measureunitDTO.setName(nameMU);
		int sem=1;
		for(MeasureunitDTO mDTO: listaMU){
			if(mDTO.getName().equals(nameMU))
				sem=0;
		}
		if(nameMU.equals(""))
			sem=0;
		if(nameMU == null)
			sem=0;
		if(sem==1)
			muDAO.insertMeasureunit(measureunitDTO);
		this.setNameMU(null);
		message = msgSuccessAdd;
	}
	/*
	 * 
	 */
	public void performDelete(ActionEvent event){
		this.measureunitDTO = (MeasureunitDTO) event.getComponent().getAttributes().get("deleteMU");
		System.out.println(measureunitDTO.getName());
		muDAO.deleteMeasureunit(measureunitDTO);
		this.measureunitDTO=null;
		message = msgSuccessDel;
	}
	
	public boolean exists(int id){
		List<ResticketDTO> ticketList = resDAO.getAllRestickets();
		for(ResticketDTO resTicket: ticketList){
			if(resTicket.getItMeasureunit()==id)
				return false;
		}
		return true;
	}
}
